//
//  TitleTabView.m
//  WeiBao
//
//  Created by macbook on 16/5/16.
//  Copyright © 2016年 ucox. All rights reserved.
//

#import "TitleTabView.h"
#import "HMSegmented.h"

#define KSegmentHeight 40.0f

@implementation TitleTabView

-(instancetype)initWithFrame:(CGRect)frame Titles:(NSArray *)titles Controllers:(NSArray *)controllers
{
    self = [super initWithFrame:frame];
    {
        [self setSegmentWithTitles:titles];
        
        [self addCollectionView];
        
        _controllersArray = controllers;
    }
    return self;
}

-(void)setSegmentWithTitles:(NSArray *)titles
{
    _segment = [[HMSegmented alloc]initWithFrame:Rect(0, 0, ScreenWidth, KSegmentHeight)];
    
    [_segment awakeFromNib];
    
    __weak typeof(self) weakSelf = self;
    
    [_segment setIndexChangeBlock:^(NSInteger index)
     {
         [weakSelf.mainCollectionView scrollRectToVisible:CGRectMake(ScreenWidth*index, 0, ScreenWidth,weakSelf.mainCollectionView.bounds.size.height) animated:YES];
     }];
    
    _segment.sectionTitles = titles;
    
    [self addSubview:_segment];
}

- (void)addCollectionView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    _mainCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, KSegmentHeight, ScreenWidth, self.bounds.size.height-KSegmentHeight) collectionViewLayout:layout];
    
    _mainCollectionView.pagingEnabled = YES;
    
    _mainCollectionView.scrollEnabled = NO;
    
    _mainCollectionView.backgroundColor =[UIColor clearColor];
    
    _mainCollectionView.delegate = self;
    
    _mainCollectionView.dataSource = self;
    
    _mainCollectionView.showsVerticalScrollIndicator = NO;
    
    _mainCollectionView.showsHorizontalScrollIndicator = NO;
    
    [_mainCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"KCell"];
    
    [self addSubview:_mainCollectionView];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _controllersArray.count;
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"KCell" forIndexPath:indexPath];
    
    [cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    UIView *view = [_controllersArray[indexPath.item] view];
    
    view.frame = cell.bounds;
        
    [cell.contentView addSubview:view];
    
    return cell;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(ScreenWidth, self.bounds.size.height - KSegmentHeight);
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger page = scrollView.contentOffset.x/ScreenWidth;
    
    [_segment setSelectedSegmentIndex:page animated:YES];
}

-(void)setSectionTitles:(NSArray *)titles
{
    _segment.sectionTitles = titles;
}
-(void)setCollectionViewScroll:(BOOL)isScroll
{
    self.mainCollectionView.scrollEnabled = isScroll;
}

@end
